<template>
	<view class="container">
		<image v-if="iconFilePath" :src="iconFilePath" mode="widthFix"></image>
		<view v-else class="image-placeholder"></view>
		<button @click="downloadImage">
			{{ progress ? `下载进度:${progress}%` : '下载图片' }}
		</button>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				imageUrl: 'https://cdn.pixabay.com/photo/2025/11/05/20/57/monastery-9939590_1280.jpg',
				iconFilePath: '',
				progress: 0,
				
			}
		},
		methods: {
			downloadImage(){
				const imagetask = uni.downloadFile({
					url: this.imageUrl,
					success: (res) => {
						if(res.statusCode === 200){
							console.log('下载成功');
							// uni.previewImage({
							// 	urls:[res.tempFilePath]
							// });
							this.iconFilePath = res.tempFilePath;
						}
					},
					fail: (err) => {
						console.error('下载失败:',err);
					}
				});
				
				imagetask.onProgressUpdate((res) => {
					this.progress = res.progress;
					console.log('下载进度:',res.progress+"%");
					console.log('已下载:',res.totalBytesWritten);
					console.log('下载进度:',res.totalBytesExpectedToWrite);
				});
			}

		
			}

	}
</script>

<style>
	body {
		margin: 0;
		background-image: url(/static/background/chenglong.jpg);
		background-repeat: no-repeat;
		background-position: center;
		background-attachment: fixed;
		background-size: cover;
	}

	button {
		margin: 5rpx;
		padding: 5rpx 65rpx;
		font-size: 1rem;
		background-color: hsl(74, 20%, 50%, 50%);
		border-radius: 15rpx;
		font-weight: bold;
		backdrop-filter: blur(52rpx);
	}

	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100vh;
	}

	image {
		width: 200px;
		height: 450rpx;
		margin: 20rpx;
		border-radius: 15rpx;

	}

	.image-placeholder {


	}

	button:hover {
		background-color: hsl(74, 20%, 60%, 50%);
	}

	button:active {
		background-color: hsl(74, 20%, 70%, 50%);
	}
</style>